//按钮点击事件
import { base, ajax, toast, print, output, printer, getBusinessInfo, cardCache, promptBox } from 'nc-lightapp-front';
import linkBudgetApi from '../../../../../public/components/linkbudget/linkbudget';
import linkVoucherApi from '../../../../../public/components/linkvoucher/linkvoucher';
import config from '../config';
import api from '../../../common/api';

const { setDefData } = cardCache;

export default function (props, key) {
    const checkedRows = props.table.getCheckedRows(config.bodycode);
    let record = null;

    switch (key) {
        case 'add':
            this.searchModal.show();
            break;

        case 'edit':
            record = checkedRows[0].data.values;
            if (checkedRows.length !== 1) {
                toast({
                    color: 'warning',
                    content: '请选中一行'
                });
                return;
            }
            props.pushTo(`/card?id=${record.pk_costshare.value}`, {
                status: 'edit',
                id: record.pk_costshare.value
            });
            break;

        case 'delete':
            this.delete();
            break;

        case 'approve':
            this.approve();
            break;

        case 'unapprove':
            this.cancelApprove();
            break;

        case 'link_bx':
            record = checkedRows[0].data.values;
            if (checkedRows.length !== 1) {
                toast({
                    color: 'warning',
                    content: '请选中一行'
                });
                return;
            }

            api.getReimbursementByLink({
                billtypeCode: '264X',
                billid: record.src_id.value,
                transtypeCode: record.djlxbm.value
            }).then((data) => {
                props.specialOpenTo(data.url, {
                    pagecode: data.data.pagecode,
                    appcode: data.data.appcode,
                    scene: 'lc',
                    status: 'browse',
                    id: data.data.id,
                    tradetype: data.data.tradetype
                });
            });
            break;

        case 'link_voucher':
            record = checkedRows[0].data.values;
            if (checkedRows.length !== 1) {
                toast({
                    color: 'warning',
                    content: '请选中一行'
                });
                return;
            }
            linkVoucherApi.link({
                data: {
                    props: props,
                    record: {
                        relationID: record.pk_costshare.value,
                        pk_billtype: record.pk_tradetype.value
                    },
                    appid: props.getSearchParam('ar')
                }
            });
            break;

        case 'link_budget':
            record = checkedRows[0].data.values;
            if (checkedRows.length !== 1) {
                toast({
                    color: 'warning',
                    content: '请选中一行'
                });
                return;
            }

            linkBudgetApi.link({
                data: {
                    tradetype: record.pk_tradetype.value,
                    openbillid: record.pk_costshare.value
                },
                success: (data) => {
                    this.setState({
                        inspectionSourceData: data,
                        isInspectionShowed: true
                    });
                }
            });
            break;

        case 'refresh':
            const searchData = props.search.getAllSearchData(config.headcode);
            if (searchData && searchData.conditions) {
                setDefData(config.searchCache, config.datasource, searchData);
            } else {
                setDefData(config.searchCache, config.datasource, {
                    logic: 'and',
                    conditions: []
                });
            }
            this.loadData();
            break;
        default:
            break;
    }
}
